home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW Related / MPW Script Tips 1.1.1 / Sample Scripts / ReplaceAll < prev    next >
Encoding:
Text File  |  1991-08-16  |  2.5 KB  |  74 lines  |  [TEXT/MPS ]

  1. #----------------------------------------------------------------------------------------------------------------------------------------------------
  2. #    ReplaceAll
  3. #    MPW Shell Script
  4. #    Written by Gina Cherry • August 16, 1991
  5. #    Copyright:    © 1991 by Apple Computer, Inc., all rights reserved.
  6. #
  7. #    Usage: 
  8. #        ReplaceAll selection newString name...
  9. #
  10. #    Function:
  11. #        ReplaceAll replaces all instances of the selection with the string newString in all files specified
  12. #        on the command line.  The selection must be  a regular expression.  If a directory is given 
  13. #        as a parameter, ReplaceAll replaces the selection with the new string in all files contained 
  14. #        in the directory and all of its subdirectories.
  15. #    
  16. #    Note:     
  17. #        The parameters must be in the order shown above.
  18. #        Directory names must end with a ':'.
  19. #        Tag variables cannot be used with ReplaceAll
  20. #        ReplaceAll may be used with the Commando option.
  21. #----------------------------------------------------------------------------------------------------------------------------------------------------
  22.  
  23. # Don't exit program on error.
  24.     Set Exit 0
  25.  
  26. # Make sure there are at least 3 parameters.
  27.     If "{#}" < 3
  28.         Echo "### {0}: too few parameters"
  29.         Echo "### Usage: {0} selection newstring files..."
  30.         Exit 1
  31.     End
  32.  
  33. # Save pattern to find and replace in Old.
  34.     Set Old "{1}"    
  35.  
  36. # Save string to replace it with in New.
  37.     Set New "{2}"
  38.  
  39. # Shift parameters by 2 so that parameters are now all file names or directories.
  40.     Shift 2
  41.  
  42.  
  43. # Loop through remaining parameters.
  44.     For item in {"parameters"}
  45.     
  46.     # Make sure each parameter is a valid file or directory.  If an invalid file or directory was given,
  47.     #    write an error message and continue processing parameters.
  48.         If !"`Exists "{item}"`"
  49.             Echo "### {0}: {item} not a valid file or directory name."  
  50.             Continue
  51.         End >> Dev:StdErr
  52.  
  53.     # List the full path name if the parameter is a text file, or list the full path names of all text files
  54.     #    in the directory and its subdirectories if the parameter is a directory.  Discard diagnostic 
  55.     #    output in bit bucket.
  56.         Set files "`Files -t TEXT -r -s -f "{item}" ≥ Dev:Null`"
  57.  
  58.     # Loop through files in the list.
  59.         For fName in {files}
  60.         # Make file the active file.
  61.             Open "{fName}" ≥ Dev:Null
  62.             
  63.             Continue If {Status} != 0
  64.             
  65.         # Position cursor at the top of the file.
  66.             Find • "{fName}"
  67.             
  68.         # Replace all instances of the selection with the new string.
  69.             Replace -c ∞ "{Old}" "{New}" "{fName}" 
  70.             
  71.         # Close the file; save changes.
  72.             Close -y "{fName}"
  73.         End
  74.     End